@app.get("/call_third_party_api/")
async def call_third_party_api(param1: str, param2: int):
    # 构建第三方接口的URL
    third_party_api_url = "https://example.com/third_party_api_endpoint"

    # 构建请求参数
    params = {
        "param1": param1,
        "param2": param2
    }

    try:
        # 调用第三方接口
        response = requests.get(third_party_api_url, params=params)
        # 检查响应状态码
        response.raise_for_status()
        # 获取第三方接口的响应数据
        third_party_response_data = response.json()
        # 返回第三方接口的响应数据给前端
        return third_party_response_data
    except requests.RequestException as e:
        # 如果调用第三方接口时发生错误，抛出HTTP异常
        raise HTTPException(status_code=500, detail="Failed to call third party API")


@app.post("/get_model_styles")
async def get_model_styles(app_key: AppKey):
    # 调用上述的接口获取模型样式列表
    url = "http://127.0.0.1:8888/std/get_style_list"
    payload = {"app_key": app_key.app_key}

    try:
        response = requests.post(url, json=payload)
        data = response.json()
        if data["success"] == "OK":
            return {"success": True, "styles": data["list"]}
        else:
            raise HTTPException(status_code=400, detail=data["error_msg"])
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/get_model_list")
async def get_model_list(app_key: AppKey):
    # 调用获取模型列表的接口
    url = "http://127.0.0.1:8888/std/get_model_list"
    payload = {"app_key": app_key.app_key}

    try:
        response = requests.post(url, json=payload)
        data = response.json()
        if data["success"] == "OK":
            return {"success": True, "models": data["list"]}
        else:
            raise HTTPException(status_code=400, detail=data["error_msg"])
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/txt2img", response_model=TextToImageResponse)
async def txt2img(request: TextToImageRequest):
    # 调用文生图接口
    url = "http://127.0.0.1:8888/std/txt2img"
    payload = request.dict()

    try:
        response = requests.post(url, json=payload)
        data = response.json()
        if data["success"] == "OK":
            return {"success": True, "list": data["list"]}
        else:
            return {"success": False, "error_code": data.get("error_code"), "error_msg": data.get("error_msg")}
    except Exception as e:
        return {"success": False, "error_msg": str(e)}


@app.post("/img2img", response_model=ImageToImageResponse)
async def img2img(request: ImageToImageRequest):
    # 调用图像到图像的接口
    url = "http://127.0.0.1:8888/std/img2img"
    payload = request.dict()

    try:
        response = requests.post(url, json=payload)
        data = response.json()
        if data["success"] == "OK":
            return {"success": True, "list": data["list"]}
        else:
            return {"success": False, "error_code": data.get("error_code"), "error_msg": data.get("error_msg")}
    except Exception as e:
        return {"success": False, "error_msg": str(e)}


@app.post("/genbg", response_model=BackgroundGenerationResponse)
async def generate_background(request: BackgroundGenerationRequest):
    # 调用背景生成接口
    url = "http://127.0.0.1:8888/std/genbg"
    payload = request.dict()

    try:
        response = requests.post(url, json=payload)
        data = response.json()
        if data["success"] == "OK":
            return {"success": True, "list": data["list"]}
        else:
            return {"success": False, "error_code": data.get("error_code"), "error_msg": data.get("error_msg")}
    except Exception as e:
        return {"success": False, "error_msg": str(e)}


@app.post("/rembg", response_model=RemoveBackgroundResponse)
async def remove_background(request: RemoveBackgroundRequest):
    # 调用背景去除接口
    url = "http://127.0.0.1:8888/std/rembg"
    payload = request.dict()

    try:
        response = requests.post(url, json=payload)
        data = response.json()
        if data["success"] == "OK":
            return {"success": True, "data": data["data"]}
        else:
            return {"success": False, "error_code": data.get("error_code"), "error_msg": data.get("error_msg")}
    except Exception as e:
        return {"success": False, "error_msg": str(e)}


@app.post("/gentxt", response_model=TextGenerationResponse)
async def generate_text(request: TextGenerationRequest):
    # 调用文本生成接口
    url = "http://127.0.0.1:8888/std/gentxt"
    payload = request.dict()

    try:
        response = requests.post(url, json=payload)
        data = response.json()
        if data["success"] == "OK":
            return {"success": True, "data": data["data"]}
        else:
            return {"success": False, "error_code": data.get("error_code"), "error_msg": data.get("error_msg")}
    except Exception as e:
        return {"success": False, "error_msg": str(e)}


@app.post("/progress", response_model=ProgressQueryResponse)
async def query_progress(request: ProgressQueryRequest):
    # 调用生成进度查询接口
    url = "http://127.0.0.1:8888/std/progress"
    payload = request.dict()

    try:
        response = requests.post(url, json=payload)
        data = response.json()
        if data["success"] == "OK":
            return {"success": True, "data": data["data"]}
        else:
            return {"success": False, "error_code": data.get("error_code"), "error_msg": data.get("error_msg")}
    except Exception as e:
        return {"success": False, "error_msg": str(e)}
